package com.fm.app03.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.fm.app03.R;

import java.util.ArrayList;
import java.util.List;

public class Price_QueryActivity extends AppCompatActivity {

    private ArrayAdapter<String> adapter;
    private List<String> dataList;

    private int count = 0;

    private String[] originalDataList = {
            "襄阳A", "襄阳B", "襄阳C", "襄阳", "北京", "上海", "广州", "深圳", "天津", "重庆", "香港", "澳门",
            "杭州", "南京", "苏州", "成都", "武汉", "西安", "长沙", "青岛",
            "大连", "沈阳", "哈尔滨", "济南", "南昌", "合肥", "福州", "厦门"
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_price_query);

        div_1();
        div_2();
        div_3();
        div_4();
        div_5();

    }

    private void div_1() {
        AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);

        // 初始化数据源
        dataList = new ArrayList<>();

        // 创建适配器并设置到AutoCompleteTextView
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, dataList);
        autoCompleteTextView.setAdapter(adapter);

        // 添加文本改变监听器
        autoCompleteTextView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 根据当前输入的文本来更新适配器数据源
                updateDataList(s.toString());
                // 通知适配器数据已更改
                adapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private void updateDataList(String inputText) {
        // 清空数据源
        dataList.clear();

        // 根据输入的文本进行筛选和匹配
        for (String item : originalDataList) {
            if (item.toLowerCase().contains(inputText.toLowerCase())) {
                dataList.add(item);
            }
        }
    }

    private void div_2() {
        AutoCompleteTextView intoDate = findViewById(R.id.intoDate);
        intoDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePickerDialog(intoDate);
            }
        });
    }

    private void div_3() {
        AutoCompleteTextView outDate = findViewById(R.id.outDate);
        outDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePickerDialog(outDate);
            }
        });
    }

    private void showDatePickerDialog(AutoCompleteTextView viewText) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // 处理选中的日期
                String selectedDate = year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日";
                viewText.setText(selectedDate);
            }
        }, year, month, day);

        datePickerDialog.show();
    }

    private void div_4() {
        Button btnAdd;
        Button btnSubtract;
        TextView tvCount;
        btnAdd = findViewById(R.id.btnAdd);
        btnSubtract = findViewById(R.id.btnSubtract);
        tvCount = findViewById(R.id.tvCount);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                tvCount.setText(String.valueOf(count));
            }
        });

        btnSubtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (count > 0) {
                    count--;
                    tvCount.setText(String.valueOf(count));
                }
            }
        });
    }

    private void div_5() {
        AutoCompleteTextView bgPrice = findViewById(R.id.bgPrice);
        bgPrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] items3 = new String[]{"￥100以下",
                        "￥100-200",
                        "￥200-400",
                        "￥400-800",
                        "￥800以上"};
                AlertDialog alertDialog3 = new AlertDialog.Builder(Price_QueryActivity.this)
                        .setTitle("请选择房间价格:")
                        .setIcon(R.mipmap.ic_launcher)
                        .setSingleChoiceItems(items3, 0, new DialogInterface.OnClickListener() {//添加列表
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                bgPrice.setText(items3[i]);
                                Toast.makeText(Price_QueryActivity.this, "点的是：" + items3[i], Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create();
                alertDialog3.show();
            }
        });
    }

}